﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSMain.Utils;
using GlxRRSDB.Data;

namespace GlxRRSMain.UserControls
{
    public partial class ImportDailyDataControl : UserControl
    {
        private const int COLUMN_CINEMA_IDX = 0;
        private const int COLUMN_DATE_IDX = 3;
        private const int COLUMN_TICKET_TYPE_IDX = 4;
        private const int COLUMN_ADMIT_IDX = 5;
        //
        private tblTicketRevenueTableAdapter x_tblTicketRevenueTableAdapter = new tblTicketRevenueTableAdapter();
        System.Data.DataSet DtSet;
        public ImportDailyDataControl()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            int count = 0;
            DataTable dtTable = DtSet.Tables[0];
            ImportTicketTypeData data = new ImportTicketTypeData();
            DateTime cachedDate = DateTime.MinValue;
            //
            for (int i = 1; i < dtTable.Rows.Count; i++)
            {
                // read cinema id/ cinema name
                string cinemaName = dtTable.Rows[i][COLUMN_CINEMA_IDX].ToString();
                if (cinemaName.ToLower().Contains("nguyen du"))
                {
                    data.CinemaID = 1001;
                }
                else if (cinemaName.ToLower().Contains("nguyen trai"))
                {
                    data.CinemaID = 1002;
                }
                else if (cinemaName.ToLower().Contains("tan binh"))
                {
                    data.CinemaID = 1003;
                }
                // read date
                string strDate = dtTable.Rows[i][COLUMN_DATE_IDX].ToString();
                if (strDate != "" && !strDate.ToLower().Contains("total"))
                {
                    data.Date = DateTime.Parse(strDate);
                }
                // read admit
                string strAdmit = dtTable.Rows[i][COLUMN_ADMIT_IDX].ToString();
                if (strAdmit == "") continue;
                int admit = int.Parse(strAdmit);
                // read ticket type
                string strTicketType = dtTable.Rows[i][COLUMN_TICKET_TYPE_IDX].ToString();
                if (strTicketType == "")
                {
                    #region insert data, clear cache
                    if (data.Date > DateTime.MinValue)
                    {
                        count++;
                        x_tblTicketRevenueTableAdapter.Insert(data);
                    }
                    // 
                    int tmpCinemaID = data.CinemaID;
                    data = new ImportTicketTypeData();
                    data.CinemaID = tmpCinemaID;
                    #endregion
                }
                else if (strTicketType.ToLower().Contains("hssv"))
                {
                    data.Student2D += admit;
                }
                else if (strTicketType.ToLower().Contains("voucher"))
                {
                    data.CompTotal2D += admit;
                }
                else if (strTicketType.ToLower().Contains("người cao tuổi"))
                {
                    data.Senior2D += admit;
                }
                else if (strTicketType.ToLower().Contains("người lớn") || strTicketType.ToLower().Contains("adult"))
                {
                    data.Adult2D += admit;
                }
                else if (strTicketType.ToLower().Contains("thường") || strTicketType.ToLower().Contains("thuong") || strTicketType.ToLower().Contains("standard"))
                {
                    data.Standard3D += admit;
                }
                else if (strTicketType.ToLower().Contains("trẻ em"))
                {
                    data.Child2D += admit;
                }
                else if (strTicketType.ToLower().Contains("vip"))
                {
                    data.VIP3D += admit;
                }
                else if (strTicketType.ToLower().Contains("happy day 3d") || strTicketType.ToLower().Contains("happy 3d"))
                {
                    data.HappyDay3D += admit;
                }
                else if (strTicketType.ToLower().Contains("happy day 2d") || strTicketType.ToLower().Contains("happy "))
                {
                    data.HappyDay2D += admit;
                }
            }
            MessageBox.Show("Imported total:" + count + " rows");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            importFileDialog.Filter = "Excel (.xlsx)|*.xlsx|All Files (*.*)|*.*";
            importFileDialog.FilterIndex = 1;
            importFileDialog.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            DialogResult userClickedOK = importFileDialog.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == DialogResult.OK)
            {
                // Open the selected file to read.
                textBox1.Text = importFileDialog.FileName;
                loadDataGrid(textBox1.Text);
            }

        }
        private void loadDataGrid(string path)
        {
            try
            {
                DtSet = FileHelper.getDataSetFromExcel(path);
                dataGridView1.DataSource = DtSet.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
